
package com.example.demojenkins.controller;

import com.example.demojenkins.entity.TableData;
import com.example.demojenkins.entity.TblOrderInfo;
import com.example.demojenkins.service.TblOrderInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (TblOrderInfo)表控制层
 *
 * @author makejava
 * @since 2024-09-15 12:50:23
 */
@RestController
@RequestMapping("/tblOrderInfo")
public class TblOrderInfoController {
    /**
     * 服务对象
     */
    @Resource
    private TblOrderInfoService tblOrderInfoService;

    /**
     * 分页查询
     *
     * @param tblOrderInfo 筛选条件
     * @param pageRequest  分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<TblOrderInfo>> queryByPage(TblOrderInfo tblOrderInfo, PageRequest pageRequest) {
        return ResponseEntity.ok(this.tblOrderInfoService.queryByPage(tblOrderInfo, pageRequest));
    }
    @PreAuthorize("hasAnyAuthority('text','admin')")
    @PostMapping(value = "/list")
    public Map<String, Object> queryByPage2(@RequestBody TableData tableData) {
        Integer page = tableData.getPageNum();
        Integer size = tableData.getPageSize();
        String orderName = tableData.getOrderName();
        if (page != null) {
            page = (page - 1) * size;
        }
        TblOrderInfo tblOrderInfo = new TblOrderInfo();
        if(StringUtils.isNotEmpty(tableData.getOrderName())){
            tblOrderInfo.setOrderName(orderName);
        }
        Long count = tblOrderInfoService.count(tblOrderInfo);
        List<TblOrderInfo> tblOrderInfos = tblOrderInfoService.queryByPage2(page, size, orderName);
        Map<String, Object> map = new HashMap<>();
        map.put("total", count);
        map.put("data", tblOrderInfos);
        return map;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<TblOrderInfo> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.tblOrderInfoService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param tblOrderInfo 实体
     * @return 新增结果
     */
    @PostMapping("/save")
    public ResponseEntity<TblOrderInfo> add(@RequestBody TblOrderInfo tblOrderInfo) {
        return ResponseEntity.ok(tblOrderInfoService.insert(tblOrderInfo));
    }

    /**
     * 编辑数据
     *
     * @param tblOrderInfo 实体
     * @return 编辑结果
     */
    @PreAuthorize("hasAnyAuthority('text','admin')")
    @PostMapping("/update")
    public ResponseEntity<TblOrderInfo> edit(@RequestBody TblOrderInfo tblOrderInfo) {
        TblOrderInfo update = this.tblOrderInfoService.update(tblOrderInfo);
        return ResponseEntity.ok(update);
    }

    /**
     * 删除数据
     *
     * @param
     * @return 删除是否成功
     */
    @PostMapping("/delete")
    public ResponseEntity<Boolean> deleteById(@RequestBody TblOrderInfo tblOrderInfo) {
        System.out.println(tblOrderInfo);
        return ResponseEntity.ok(this.tblOrderInfoService.deleteById(tblOrderInfo.getId()));
    }

    @DeleteMapping("/{ids}")
    public ResponseEntity<List<Integer>> deleteByIds(@PathVariable List<Integer> ids) {
        System.out.println(ids);
        this.tblOrderInfoService.deleteByIds(ids);
        return ResponseEntity.ok(ids);
    }

}

